package com.woniuxy.auth.config;/*
@author houguai
@create 2021-03-04 14:38
*/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.auth.entity.Rbac_perm;
import com.woniuxy.auth.mapper.Rbac_permMapper;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.Map;

public class PermMapFactoryBean implements FactoryBean<Map<String,String>> {

    @Autowired
    private Rbac_permMapper permMapper;

    @Override
    public Map<String, String> getObject()  {
        //配置请求的URL与过滤器的关系
        Map<String, String> map = new LinkedHashMap<>();
        map.put("auth", "anon");
        map.put("/manager/login", "anon");
        map.put("/menu/get", "jwt");

        QueryWrapper<Rbac_perm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "a");
        queryWrapper.eq("status", "y");
        //重新进行字符串进行拼接
        permMapper.selectList(queryWrapper).forEach(p -> {
            map.put("/" + p.getLink(), "jwt,perms[" + p.getCode() + "]");
        });
        return map;

    }

    @Override
    public Class<?> getObjectType() {
        return Map.class;
    }
}
